package com.anti.modular.sys.pos.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.anti.commom.core.exception.BusinessException;
import com.anti.commom.enums.CommonStatusEnum;
import com.anti.core.context.login.LoginContextHolder;
import com.anti.jdbc.DBUtils;
import com.anti.jdbc.support.page.PageParam;
import com.anti.jdbc.support.page.PageResult;
import com.anti.modular.sys.emp.service.SysEmpExtOrgPosService;
import com.anti.modular.sys.emp.service.SysEmpPosService;
import com.anti.modular.sys.pos.dao.SysPosDao;
import com.anti.modular.sys.pos.entity.SysPos;
import com.anti.modular.sys.pos.param.SysPosParam;
import com.anti.modular.sys.pos.service.SysPosService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description TODO
 * @Author anTi
 * @Date 2021-3-11
 */
@Service
public class SysPosServiceImpl implements SysPosService {
    @Resource
    DBUtils dbUtils;
    @Resource
    SysEmpPosService sysEmpPosService;
    @Resource
    SysEmpExtOrgPosService sysEmpExtOrgPosService;
    @Resource
    SysPosDao sysPosDao;

    @Override
    public SysPos getById(Long posId) {
        String sql="select * from sys_pos where id =? ";
        return dbUtils.getFirst(sql,SysPos.class,posId);
    }

    @Override
    public PageResult page(PageParam pageParam, SysPosParam sysPosParam) {
        StringBuilder sb=new StringBuilder("select * from sys_pos where status=0 ");
        List<Object> params=new ArrayList<>();
        if (ObjectUtil.isNotNull(sysPosParam)) {
            //根据职位名称模糊查询
            if (ObjectUtil.isNotEmpty(sysPosParam.getName())) {
                sb.append(" and name like ? ");
                params.add("%"+sysPosParam.getName()+"%");
            }
            //根据职位编码模糊查询
            if (ObjectUtil.isNotEmpty(sysPosParam.getCode())) {
                sb.append(" and code like ? ");
                params.add("%"+sysPosParam.getCode()+"%");
            }
        }
        sb.append(" order by sort asc");
        PageResult result = dbUtils.query(pageParam, sb.toString(), params.toArray());
        return result;
    }

    @Override
    public List<SysPos> list(SysPosParam sysPosParam) {
        StringBuilder sb=new StringBuilder("select * from sys_pos where status=0 ");
        List<Object> params=new ArrayList<>();
        if (ObjectUtil.isNotNull(sysPosParam)) {
            //根据职位编码模糊查询
            if (ObjectUtil.isNotEmpty(sysPosParam.getCode())) {
                sb.append(" and code = ? ");
                params.add(sysPosParam.getCode());
            }
        }
        sb.append(" order by sort asc");
        return dbUtils.query(sb.toString(),SysPos.class,params.toArray());
    }

    @Override
    public void add(SysPosParam sysPosParam) {
        //校验参数，检查是否存在相同的名称和编码
        checkParam(sysPosParam, false);
        SysPos sysPos = new SysPos();
        BeanUtil.copyProperties(sysPosParam, sysPos);
        sysPos.setStatus(CommonStatusEnum.ENABLE.getCode());
        sysPos.setCreateTime(new Date());
        sysPos.setCreateUser(LoginContextHolder.me().getSysLoginUserId());
        sysPosDao.save(sysPos);
    }

    @Override
    public void delete(SysPosParam sysPosParam) {
        SysPos sysPos = this.querySysPos(sysPosParam);
        Long id = sysPos.getId();
        //该职位下是否有员工
        boolean hasPosEmp = sysEmpPosService.hasPosEmp(id);
        //只要还有，则不能删
        if (hasPosEmp) {
            throw new BusinessException(0,"该职位下有员工，无法删除");
        }
        //该附属职位下是否有员工
        boolean hasExtPosEmp = sysEmpExtOrgPosService.hasExtPosEmp(id);
        //只要还有，则不能删
        if (hasExtPosEmp) {
            throw new BusinessException(0,"该职位下有员工，无法删除");
        }
        sysPosDao.delete(sysPosParam.getId());
    }

    @Override
    public void edit(SysPosParam sysPosParam) {
        SysPos sysPos = this.querySysPos(sysPosParam);
        //校验参数，检查是否存在相同的名称和编码
        checkParam(sysPosParam, true);
        BeanUtil.copyProperties(sysPosParam, sysPos);
        //不能修改状态，用修改状态接口修改状态
        sysPos.setUpdateTime(new Date());
        sysPos.setUpdateUser(LoginContextHolder.me().getSysLoginUserId());
        sysPosDao.update(sysPos);
    }

    @Override
    public SysPos detail(SysPosParam sysPosParam) {
        return this.querySysPos(sysPosParam);
    }

    /**
     * 获取系统职位
     *
     * @author xuyuxiang
     * @date 2020/3/26 9:56
     */
    private SysPos querySysPos(SysPosParam sysPosParam) {
        SysPos sysPos = dbUtils.getFirst("SELECT * FROM SYS_POS WHERE ID=?",SysPos.class,sysPosParam.getId());
        if (ObjectUtil.isNull(sysPos)) {
            throw new BusinessException(0,"职位不存在");
        }
        return sysPos;
    }
    /**
     * 校验参数，检查是否存在相同的名称和编码
     */
    private void checkParam(SysPosParam sysPosParam, boolean b) {
        Long id = sysPosParam.getId();
        String name = sysPosParam.getName();
        String code = sysPosParam.getCode();
        List<Object> params=new ArrayList();
        StringBuilder sb=new StringBuilder("select count(*) from sys_pos where status=0 and name=? and code=? ");
        params.add(name);
        params.add(code);
        if (b) {
            sb.append(" and id <> ? ");
            params.add(id);
        }

        int countByName = dbUtils.getFirst(sb.toString(),Integer.class,params.toArray());
        if (countByName >= 1) {
            throw new BusinessException(0,"名称或编码已存在！");
        }
    }

}
